package com.framework.spring.context.bfpp;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.stereotype.Component;

@Component
@Slf4j(topic = "e")
public class A implements BeanFactoryPostProcessor {

	public A() {
		log.info("A construct");
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		log.info("scan A#BeanFactoryPostProcessor.postProcessBeanFactory");
		DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory;
		// 此时 k的postProcessBeanFactory 无法回调
		AbstractBeanDefinition kBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition(K.class).getBeanDefinition();
		defaultListableBeanFactory.registerBeanDefinition("k", kBeanDefinition);

//		// 此时 x中的@Bean没有生效
//		AbstractBeanDefinition xBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition(X.class).getBeanDefinition();
//		defaultListableBeanFactory.registerBeanDefinition("x", xBeanDefinition);
	}
}
